from django.db import models
from drf_shop_admin.utils.models import BaseModel

# Create your models here.
class Permissions(BaseModel):
    """
    权限表
    """
    METHOD_CHOICES = (
        (u'POST', u'增'),
        (u'DELETE', u'删'),
        (u'PUT', u'改'),
        (u'PATCH', u'局部改'),
        (u'GET', u'查')
    )

    name = models.CharField(max_length=30, verbose_name='权限名')
    sign = models.CharField(max_length=30, unique=True, verbose_name='权限标识')
    menu = models.BooleanField(verbose_name='是否为菜单')  # True为菜单,False为接口
    method = models.CharField(max_length=8, blank=True, default='', choices=METHOD_CHOICES, verbose_name='方法')
    path = models.CharField(max_length=200, blank=True, default='', verbose_name='请求路径正则')
    desc = models.CharField(max_length=30, blank=True, default='', verbose_name='权限描述')
    pid = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, verbose_name='父权限')

    objects = models.Manager()

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '权限'
        verbose_name_plural = verbose_name
        ordering = ['id']

class Roles(BaseModel):
    """
    角色表
    """
    name = models.CharField('角色名称', max_length=32, unique=True, blank=False, null=False)
    desc = models.CharField('角色描述', max_length=100, blank=True, default='')
    permissions = models.ManyToManyField('Permissions', related_name='role', verbose_name='角色权限', blank=True)

    objects = models.Manager()

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '角色'
        verbose_name_plural = verbose_name
        ordering = ['id']